#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "FileListDetectorPage.h"
#include "FileListDetectorDlg.h"

#include "../client/SettingsManager.h"
#include "../client/ClientProfileManager.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define BUFLEN 256

#define SAVECOMBOS(a,b) \
	cRaw = GetDlgItem(a); \
	SettingsManager::getInstance()->set(b, cRaw.GetCurSel());

PropPage::TextItem FileListDetectorPage::texts[] = {
	{ IDC_ADD, ResourceManager::ADD },
	{ IDC_CHANGE, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE, ResourceManager::REMOVE },
	// potheads starts svn183
	{ IDC_SETTINGS_ADL_FAIL_RECHECK, ResourceManager::SETTINGS_ADLS_FAIL_RECHECK },
	{ IDC_SETTINGS_HOURS_0_IS_OFF, ResourceManager::SETTINGS_HOURS_0_IS_OFF },
	// pothead ends
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item FileListDetectorPage::items[] = {
	{ IDC_PERCENT_HASHED, SettingsManager::PERCENT_HASHED, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_ADL_FAIL_HOURS, SettingsManager::ADL_FAIL_HOURS, PropPage::T_INT_SHOWDEFAULT },
	{ 0, 0, PropPage::T_END }
};

LRESULT FileListDetectorPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items);

	CRect rc;
	
	//Sulan start svn209	
	cRaw.Attach(GetDlgItem(IDC_HASH_ERROR_RAW));
	fillCombo();
	cRaw.SetCurSel(SETTING(HASH_ERROR_RAW));
	cRaw.Detach();
	cRaw.Attach(GetDlgItem(IDC_FAKE_FILE_LIST_RAW));
	fillCombo();
	cRaw.SetCurSel(SETTING(FAKE_FILE_LIST_RAW));
	cRaw.Detach();
	cRaw.Attach(GetDlgItem(IDC_FAKE_FILE_LIST_RAW2));
	fillCombo();
	cRaw.SetCurSel(SETTING(TAG_FILELIST_GEN_MISMATCH));
	cRaw.Detach();
	//End
	// pothead starts svn219
	cRaw.Attach(GetDlgItem(IDC_ADL_LOW_POINTS));
	fillCombo();
	cRaw.SetCurSel(SETTING(ADL_RAW_LOW_POINTS));
	cRaw.Detach();
	cRaw.Attach(GetDlgItem(IDC_ADL_MEDIUM_POINTS));
	fillCombo();
	cRaw.SetCurSel(SETTING(ADL_RAW_MEDIUM_POINTS));
	cRaw.Detach();
	cRaw.Attach(GetDlgItem(IDC_ADL_HIGH_POINTS));
	fillCombo();
	cRaw.SetCurSel(SETTING(ADL_RAW_HIGH_POINTS));
	cRaw.Detach();
	// pothead ends

	ctrlProfiles.Attach(GetDlgItem(IDC_FILELIST_DETECTOR_LIST));
	ctrlProfiles.GetClientRect(rc);

	ctrlProfiles.InsertColumn(0, CTSTRING(DETECT_STRING), LVCFMT_LEFT, (rc.Width() / 9)*2, 0);
	ctrlProfiles.InsertColumn(1, CTSTRING(CHEATING_DESCRIPTION), LVCFMT_LEFT, (rc.Width() / 9)*3, 1);
	ctrlProfiles.InsertColumn(2, CTSTRING(ACTION), LVCFMT_LEFT, (rc.Width() / 9)*2, 2);
	ctrlProfiles.InsertColumn(3, CTSTRING(CLIENT), LVCFMT_LEFT, (rc.Width() / 9)*2, 3);

	if(BOOLSETTING(FULL_ROW_SELECT))
		ctrlProfiles.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);

	// Do specialized reading here

	//Add the info here
	FileListDetectorProfile::List lst = ClientProfileManager::getInstance()->getFileListDetectors();

	for(FileListDetectorProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
		FileListDetectorProfile& fd = *i;	
		addEntry(fd, ctrlProfiles.GetItemCount());
	}

	return TRUE;
}

LRESULT FileListDetectorPage::onAdd(WORD , WORD , HWND , BOOL& ) {
	FileListDetectorDlg dlg;
	dlg.currentProfileId = -1;
	
	if(dlg.DoModal() == IDOK) {
		addEntry(ClientProfileManager::getInstance()->addFileListDetector(		
			dlg.name, 
			dlg.cheatingDescription,
			dlg.rawToSend 
			//Sulan start svn120
			, dlg.client
			//End
			), ctrlProfiles.GetItemCount());
		
	}
	return 0;
}

LRESULT FileListDetectorPage::onChange(WORD , WORD , HWND , BOOL& ) {
	if(ctrlProfiles.GetSelectedCount() == 1) {
		int sel = ctrlProfiles.GetSelectedIndex();
		
		FileListDetectorDlg dlg;

		dlg.currentProfileId = ctrlProfiles.GetItemData(sel);

		if(dlg.DoModal() == IDOK) {
			ctrlProfiles.SetItemText(dlg.currentProfileId, 0, dlg.name.c_str());
			ctrlProfiles.SetItemText(dlg.currentProfileId, 1, dlg.cheatingDescription.c_str());
			
			dlg.currentProfile.setName(dlg.name);
			dlg.currentProfile.setCheatingDescription(dlg.cheatingDescription);
			dlg.currentProfile.setRawToSend(dlg.rawToSend);
			//Sulan start svn120
			dlg.currentProfile.setClient(dlg.client);
			//End
			ClientProfileManager::getInstance()->updateFileListDetector(dlg.currentProfile);
		}
		ctrlProfiles.SetRedraw(FALSE);
		ctrlProfiles.DeleteAllItems();
		FileListDetectorProfile::List lst = ClientProfileManager::getInstance()->getFileListDetectors();
		for(FileListDetectorProfile::Iter j = lst.begin(); j != lst.end(); ++j) {
			FileListDetectorProfile& fd = *j;	
			addEntry(fd, ctrlProfiles.GetItemCount());
		}
		ctrlProfiles.SelectItem(sel);
		ctrlProfiles.SetRedraw(TRUE);
	}
	return 0;
}

LRESULT FileListDetectorPage::onRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlProfiles.GetSelectedCount() == 1) {
		int i = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED);
		ClientProfileManager::getInstance()->removeFileListDetector(ctrlProfiles.GetItemData(i));
		ctrlProfiles.DeleteItem(i);
	}
	return 0;
}

void FileListDetectorPage::write() {
	CComboBox cRaw;
	SAVECOMBOS(IDC_HASH_ERROR_RAW, SettingsManager::HASH_ERROR_RAW);
	SAVECOMBOS(IDC_FAKE_FILE_LIST_RAW, SettingsManager::FAKE_FILE_LIST_RAW);
	// pothead starts svn135 svn216 svn219
	SAVECOMBOS(IDC_FAKE_FILE_LIST_RAW2, SettingsManager::TAG_FILELIST_GEN_MISMATCH);
	SAVECOMBOS(IDC_ADL_LOW_POINTS, SettingsManager::ADL_RAW_LOW_POINTS);
	SAVECOMBOS(IDC_ADL_MEDIUM_POINTS, SettingsManager::ADL_RAW_MEDIUM_POINTS);
	SAVECOMBOS(IDC_ADL_HIGH_POINTS, SettingsManager::ADL_RAW_HIGH_POINTS);
	// pothead ends
	ClientProfileManager::getInstance()->saveClientProfiles();
	PropPage::write((HWND)*this, items);
}
void FileListDetectorPage::addEntry(const FileListDetectorProfile& fd, int pos) {
	StringList lst;
	lst.push_back(fd.getName());
	lst.push_back(fd.getCheatingDescription());
	lst.push_back(RAWSETTING(fd.getRawToSend()));
	//Sulan start svn120
	lst.push_back(fd.getClient());
	//End
	ctrlProfiles.insert(pos, lst, 0, (LPARAM)fd.getId());
}

/**
 * @file
 * $Id: FileListDetectorPage.cpp Last changed: 2005-07-14 23:40:33 By: Pothead for SVN: 249 Exp$ */